package com.web.wefun.util;

import com.baidubce.http.ApiExplorerClient;
import com.baidubce.http.AppSigner;
import com.baidubce.http.HttpMethodName;
import com.baidubce.model.ApiExplorerRequest;
import com.baidubce.model.ApiExplorerResponse;


public class PhoneCodeUtil {

    public static String getCode(String phone){
        String path = "https://dfsms.api.bdymkt.com/send_sms";
        ApiExplorerRequest request = new ApiExplorerRequest(HttpMethodName.POST, path);
        request.setCredentials("eeafe85471614fab9b382f21be3f25b2",
                "1a5daeb0c6814369abebefdea547225a");

        // 设置header参数
        request.addHeaderParameter("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

        //生成4位数数值
        int num1 = (int)(Math.random()*10);
        int num2 = (int)(Math.random()*10);
        int num3 = (int)(Math.random()*10);
        int num4 = (int)(Math.random()*10);
        String phoneCode = ""+num1+num2+num3+num4;
        // 设置jsonBody参数
//        String jsonBody = "phone_number=13480849706&template_id=TPL_0001&content=code:6666,expire_at:5";
        String jsonBody = "phone_number="+phone+"&template_id=TPL_0001&content=code:"+phoneCode+",expire_at:5";
        request.setJsonBody(jsonBody);

        ApiExplorerClient client = new ApiExplorerClient(new AppSigner());

        try {
            ApiExplorerResponse response = client.sendRequest(request);
            // 返回结果格式为Json字符串
            //System.out.println(response.getResult());
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("phoneCode:"+phoneCode);
        return  phoneCode;
    }

}
